import { ICommonParams, IHeaders } from '../../interfaces';
import { close, open } from '../_redis';
import { get_use_info, IRole } from './_get-user-info';

/**
 * 获取当前角色信息
 * @param msg 系统参数，fd.data.msg
 * @param headers 系统参数，fd.data.hearders
 * @param productid 项目productid
 * @param clienttype 端点类型，web：3,h5：2,xcx:9
 */
export default async function get_current_role_info(msg: ICommonParams, headers: IHeaders, productid: string, clienttype: string) {
	const client = open();
	try {
		const cookie = msg.cookie;
		const sessionid = cookie.sessionid;
		const user_info = await get_use_info(client, headers.actionid, sessionid, headers.spaceid, productid, clienttype);
		const role_no = user_info.role_no;
		const role_info = user_info.roles.filter((item) => {
			return item.role_no === role_no;
		}) as IRole[];
		if (role_info.length === 0) {
			return {};
		} else {
			return role_info[0];
		}
	} finally {
		close(client);
	}
}
